home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / UTextScroller.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  5.8 KB  |  284 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // UTextScroller.cp
  3.  
  4. #include "UTextScroller.h"
  5.  
  6. #pragma segment MyViewTools
  7.  
  8. //----------------------------------------------------------------
  9. class TSuperScrollBar : public TScrollerScrollBar {
  10.     public:
  11.         virtual pascal void SetLongVal(VCoordinate itsVal, Boolean redraw);
  12.         virtual pascal void DeltaValue(VCoordinate delta);
  13. };
  14.  
  15. pascal void TSuperScrollBar::SetLongVal(VCoordinate itsVal,
  16.                                 Boolean redraw)
  17. {
  18.     itsVal = Max(fLongMin, itsVal);
  19.     if (itsVal != fLongVal)
  20.     {
  21.         fLongVal = itsVal;
  22.         itsVal = Min(fLongVal, fLongMax); // postpone max-check
  23.         this->SetVal((short)(itsVal >> fBitsToShift), redraw);
  24.     }
  25. }
  26.  
  27. pascal void TSuperScrollBar::DeltaValue(VCoordinate delta)
  28. {
  29.     if (delta != 0)
  30.         this->SetLongVal(fLongVal, TRUE); // SetLongVal does index check
  31. }
  32.  
  33.  
  34. //----------------------------------------------------------------
  35.  
  36. TTextScroller::TTextScroller()
  37. {
  38. }
  39.  
  40. pascal void TTextScroller::Initialize()
  41. {
  42.     inherited::Initialize();
  43. }
  44.  
  45. pascal void TTextScroller::IRes(TDocument *itsDocument,
  46.                              TView *itsSuperView,
  47.                              TStream *itsParams)
  48. {
  49.     inherited::IRes(itsDocument, itsSuperView, itsParams);
  50. }
  51.  
  52. pascal void TTextScroller::Free()
  53. {
  54.     inherited::Free();
  55. }
  56.  
  57. pascal void TTextScroller::CreateScrollBar(VHSelect itsDirection)
  58. {
  59.     if (itsDirection == hSel)
  60.     {
  61.         inherited::CreateScrollBar(itsDirection);
  62.         return;
  63.     }
  64.     FailInfo fi;
  65.     if (fi.Try())
  66.     {
  67.         TSuperScrollBar *ssb = new TSuperScrollBar;
  68.         ssb->IScrollerScrollBar(fSuperView, gZeroVPt, gZeroVPt, 
  69.                 sizeVariable, sizeVariable, itsDirection, 
  70.                 fMaxTranslation[itsDirection], this);
  71.         fi.Success();
  72.     }
  73.     else    // Recover
  74.     {
  75.         this->Free();
  76.         fi.ReSignal();
  77.     }
  78. }
  79.  
  80. void TTextScroller::DoScrollUp()
  81. {
  82.     gApplication->InvalidateHelpRgn();
  83.     ScrollBy(VPoint(0, -fScrollUnit.v), kRedraw);
  84.     Update();
  85. }
  86.  
  87. void TTextScroller::DoScrollDown()
  88. {
  89.     gApplication->InvalidateHelpRgn();
  90.     TScrollerScrollBar *sb = fScrollBars[vSel];
  91.     if (sb->fLongVal >= sb->fLongMax)
  92.         return;
  93.     ScrollBy(VPoint(0, fScrollUnit.v), kRedraw);
  94.     Update();
  95. }
  96.  
  97. void TTextScroller::DoPageUp()
  98. {
  99.     gApplication->InvalidateHelpRgn();
  100.     fScrollBars[vSel]->DeltaValue(VCoordinate(ScrollStep(vSel, inPageUp)));
  101.     Update();
  102. }
  103.  
  104. void TTextScroller::DoPageDown()
  105. {
  106.     gApplication->InvalidateHelpRgn();
  107.     TScrollerScrollBar *sb = fScrollBars[vSel];
  108.     if (sb->fLongVal >= sb->fLongMax)
  109.         return;
  110.     fScrollBars[vSel]->DeltaValue(VCoordinate(ScrollStep(vSel, inPageDown)));
  111.     Update();
  112. }
  113.  
  114. void TTextScroller::DoGoHome()
  115. {
  116.     gApplication->InvalidateHelpRgn();
  117.     SetLocalOrigin(gZeroVPt, kRedraw);
  118.     Update();
  119. }
  120.  
  121. void TTextScroller::DoGoEnd()
  122. {
  123.     gApplication->InvalidateHelpRgn();
  124.     SetLocalOrigin(VPoint(0, fMaxTranslation.Copy().v), kRedraw);
  125.     Update();
  126. }                
  127.  
  128. pascal void TTextScroller::DoKeyEvent(TToolboxEvent *event)
  129. {
  130.     if (!IsEnabled() || !fRespondsToFunctionKeys)
  131.     {
  132.         inherited::DoKeyEvent(event);
  133.         return;
  134.     }
  135.     switch (event->fCharacter)
  136.     {
  137.         case chUp:
  138.             if (!event->IsOptionKeyPressed())
  139.                 DoScrollUp();
  140.             break;
  141.             
  142.         case chDown:
  143.             if (!event->IsOptionKeyPressed())
  144.                 DoScrollDown();
  145.             break;
  146.  
  147.         case chPageUp:
  148.             DoPageUp();
  149.             break;
  150.             
  151.         case chPageDown:
  152.             DoPageDown();
  153.             break;
  154.             
  155.         case chHome:
  156.             DoGoHome();
  157.             break;                
  158.         case chEnd:
  159.             DoGoEnd();
  160.             break;
  161.             
  162.         default:
  163.             inherited::DoKeyEvent(event);
  164.             break;
  165.     }
  166. }
  167.  
  168. pascal void TTextScroller::DoCommandKeyEvent(TToolboxEvent *event)
  169. {
  170.     if (!IsEnabled() || !fRespondsToFunctionKeys)
  171.     {
  172.         inherited::DoKeyEvent(event);
  173.         return;
  174.     }
  175.     switch (event->fCharacter)
  176.     {
  177.         case chUp:
  178.             if (event->IsCommandKeyPressed() && event->IsOptionKeyPressed())
  179.                 DoGoHome();
  180.             else if (event->IsCommandKeyPressed() && !event->IsOptionKeyPressed())
  181.                 DoPageUp();
  182.             else
  183.                 inherited::DoCommandKeyEvent(event);
  184.             break;
  185.  
  186.         case chDown:
  187.             if (event->IsCommandKeyPressed() && event->IsOptionKeyPressed())
  188.                 DoGoEnd();
  189.             else if (event->IsCommandKeyPressed() && !event->IsOptionKeyPressed())
  190.                 DoPageDown();
  191.             else
  192.                 inherited::DoCommandKeyEvent(event);
  193.             break;
  194.  
  195.         default:
  196.             inherited::DoCommandKeyEvent(event);
  197.     }
  198. }
  199.  
  200. pascal VCoordinate TTextScroller::ScrollStep(VHSelect vhs,
  201.                                          short partCode)
  202. {
  203.     short scrollUnit = (short)fScrollUnit[vhs];
  204.     VCoordinate delta;
  205.     VCoordinate adjustment;
  206.     VPoint deltaPt = gZeroVPt;
  207.  
  208.     switch (partCode)
  209.     {
  210.         case inUpButton:
  211.         case inDownButton:
  212.             delta = scrollUnit;
  213.             break;
  214.             
  215.         case inPageUp:
  216.         case inPageDown:
  217.             delta = fSize[vhs] - scrollUnit;
  218.             break;
  219.             
  220. #if qDebug
  221.         default:
  222.             {
  223.             CStr255 theString;
  224.             ConcatNumber("TScroller::ScrollStep: bad part code =", partCode, theString);
  225.             ProgramBreak(theString);
  226.             break;
  227.             }
  228. #endif
  229.  
  230.     }
  231.  
  232.     if ((partCode == inUpButton) || (partCode == inPageUp))
  233.         delta = -delta;
  234.  
  235.     // Constrain if necessary 
  236.     if (fConstrain[vhs] && (scrollUnit != 0))
  237.     {
  238.         adjustment = (Max(0, fTranslation[vhs] + delta)) % scrollUnit;
  239.         if (adjustment != 0)
  240.             if (delta > 0)
  241.                 delta -= adjustment;
  242.             else
  243.                 delta += (scrollUnit - adjustment);
  244.     }
  245.     deltaPt[vhs] = delta;
  246.  
  247.     DoScroll(deltaPt, kRedraw);
  248.     return delta;
  249. }
  250.  
  251.  
  252. pascal void TTextScroller::DoScroll(const VPoint &delta,
  253.                                 Boolean redraw)
  254. {
  255.     VPoint localDelta(delta);
  256.  
  257.     for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  258.     {
  259.         if (localDelta[vhs] < 0)
  260.         {
  261.             // !!!FIX the following 2 lines after MPW C compiler bug is corrected.
  262.             long temp = Max(localDelta[vhs], -fTranslation[vhs]);
  263.             localDelta[vhs] = temp;
  264.         }
  265.         else if (localDelta[vhs] > 0)
  266.         {
  267.             // !!!FIX the following 2 lines after MPW C compiler bug is corrected.
  268.             long temp1 = Min(localDelta[vhs], (fMaxTranslation[vhs] /* - fTranslation[vhs] */));
  269.             localDelta[vhs] = temp1;
  270.         }
  271.         fTranslation[vhs] += localDelta[vhs];
  272.     }
  273.  
  274.     if (localDelta != gZeroVPt)
  275.     {
  276.         this->InvalidateFocus();
  277.         this->UpdateCoordinates();
  278.  
  279.         // !!! In the future the decision will be made differently as to how to render
  280.         if (redraw)
  281.             this->ScrollDraw(localDelta, kInvalidate);
  282.     }
  283. }
  284.